﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void bt_Rigister_Click(object sender, EventArgs e)
    {

        //MessageBox.Show(this.Page,"xixi", "haha",MessageBoxButtons.YesNo);
        String user = TextBox4.Text;
        String pw = TextBox7.Text;
        String pw2 = TextBox1.Text;
        String connString = "server=localhost;database=test;uid=root;pwd=123456";
        MySqlConnection conn = new MySqlConnection(connString);
        conn.Open();
        String sqlselect = "select username from test.webuser where username = '" + user + "';";
        MySqlCommand mycommand = new MySqlCommand(sqlselect, conn);
        MySqlDataReader mdr = mycommand.ExecuteReader();
        if (user == "")
        {
            Response.Write("<script>alert('账号不能为空')</script>");
        }
        else if (mdr.Read())
        {
            Response.Write("<script>alert('此用户名被占用，注册失败');window.location.href ='http://localhost:50015/Register.aspx'</script>");
        }
        else if (pw == "")
        {
            Response.Write("<script>alert('密码不能为空')</script>");
        }
        else if(pw2 == ""||!pw2.Equals(pw))
        {
            Response.Write("<script>alert('两次输入的密码不相同')</script>");
        }
        else
        { 
           try
           {
               conn.Close();
               conn.Open();
               String sql = "INSERT INTO webuser(username,password) VALUES('" + user + "','" + pw + "')";
               MySqlCommand cmd = new MySqlCommand(sql, conn);
               cmd.ExecuteNonQuery();
               String t = DateTime.Now.ToString();
               String sql2 = "update test.webuser set registertime='" + t + "' where username = '" + user + "';";
               MySqlCommand cmd2 = new MySqlCommand(sql2, conn);
               cmd2.ExecuteNonQuery();
               conn.Close();
               Response.Write("<script>alert('注册成功');window.location='http://localhost:50015/Login.aspx';</script>");
           }
           catch
           {
               conn.Close();
               Response.Write("<script>alert('注册失败，联系管理员修复')</script>");
           }
        }
    }

    protected void bt_back_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://localhost:50015/Login.aspx", true);
    }
}